package com.cms.pub;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.metadata.persistence.SYS_DICTS;
import com.pub.util.StringFunction;

/**
 * 2008-10-4
 */
public class HTXMDataSet2JsonUtil {
	public static JSONObject convertDataSet2Json(EFDataSet dataset, EFRowSet dictRowSet) {
		EFRowSet  parentRowSet = EFRowSet.getInstance();
		JSONObject     jsonObj = new JSONObject();//创建json格式的数据
		JSONArray      jsonArr = new JSONArray();//json格式的数组
		jsonObj.put("data", jsonArr);//再将这个json格式的的数组放到最终的json对象中。
		convertDataSet2Json(parentRowSet, "", dataset, 0, jsonObj, jsonArr, dictRowSet);
		if(jsonArr.size() > 0) {
			jsonObj.put("data", jsonArr);
		}
		return jsonObj;
	}
	
	private static int convertDataSet2Json(EFRowSet parentRowSet, String parentXMBH, EFDataSet dataset, 
			                               int index, JSONObject jsonObj, JSONArray jsonArr, EFRowSet dictRowSet) {
		Map<String, String>     jsMap = new HashMap<String, String>();
		int                struLength = 0;
		int                      F_JS = 0;
		String             DCT_BMSTRU = dictRowSet.getString("DCT_BMSTRU", "");
		
		jsMap.put("1", "b");
		jsMap.put("2", "c");
		jsMap.put("3", "d");
		jsMap.put("4", "e");
		jsMap.put("5", "f");
		JSONObject jsonObj1 = new JSONObject();//创建json格式的数据
		for(; index < dataset.getRowCount(); index++) {
			EFRowSet rowset = dataset.getRowSet(index);
			F_JS = rowset.getNumber(dictRowSet.getString(SYS_DICTS._DCT_JSCOLID_, ""), 0).intValue();
			
			jsonObj1 = new JSONObject();
			jsonObj1.put("text", rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_MCCOLID_, ""), ""));
			jsonObj1.put(dictRowSet.getString(SYS_DICTS._DCT_MCCOLID_, ""), rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_MCCOLID_, ""), ""));
			jsonObj1.put(dictRowSet.getString(SYS_DICTS._DCT_BMCOLID_, ""), rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_BMCOLID_, ""), ""));
			jsonObj1.put(dictRowSet.getString(SYS_DICTS._DCT_JSCOLID_, ""), rowset.getObject(dictRowSet.getString(SYS_DICTS._DCT_JSCOLID_, ""), "0"));
			jsonObj1.put(dictRowSet.getString(SYS_DICTS._DCT_MXCOLID_, ""), rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_MXCOLID_, ""), ""));
			jsonObj1.put("icon", jsMap.get(rowset.getObject(dictRowSet.getString(SYS_DICTS._DCT_JSCOLID_, ""), "0").toString()));
			if(F_JS == 1) {
				struLength = 0;
			} else {
				struLength = StringFunction.GetStructLength(DCT_BMSTRU, F_JS - 1);
			}
			if(rowset.getString("F_MX", "").equals("1") && (parentXMBH.equals("") || rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_BMCOLID_, ""), "").substring(0, struLength).equals(parentXMBH))) {
				jsonArr.add(jsonObj1);
			} else if(!parentXMBH.equals("") && !rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_BMCOLID_, ""), "").substring(0, struLength).equals(parentXMBH)) {
				return index - 1;
			} else {
				JSONArray jsonArr2 = new JSONArray();//json格式的数组
				index = convertDataSet2Json(rowset, rowset.getString(dictRowSet.getString(SYS_DICTS._DCT_BMCOLID_, ""), ""), dataset, index + 1, jsonObj1, jsonArr2, dictRowSet);				
				if(jsonArr2.size() > 0) {
					jsonObj1.put("children", jsonArr2);
				}
				jsonArr.add(jsonObj1);
			}
		}
		return index - 1;
	}
	
}
